<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\File;
use Illuminate\Support\Facades\Storage;

class DropController extends Controller {

    public function index() {
        return view('index');
    }

    public function show($id) {
        $file = File::where('ukey', $id)->first();
        if(!$file) abort(404);
        return response()->file(storage_path('app/' . $file->path));
    }

    public function upload() {
        $file = request()->file('file');
        if($file) {
            $redirect = true;
            if($file->getClientSize() > 1024*1024*5) $redirect = false;

            $path = $file->store('public/' . date('Y/md'));
            $file = new File;
            $file->path = $path;
            $file->save();
            $file->ukey = id_to_ukey($file->id);
            $file->save();

            return response()->json([
                'r' =>  1,
                'ukey'  =>  $file->ukey,
                'redirect'  =>  $redirect
            ]);
        }
    }
}
